//
//  SinaHomeComposeView.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/23.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaHomeComposeView.h"
#import "SinaHomeComposeController.h"
#import "SinaHomeComposeButton.h"
#import <POP.h>

@interface SinaHomeComposeView ()
@property (nonatomic,weak) UIImageView *backImageV;
@property(nonatomic,strong) NSMutableArray<SinaHomeComposeButton *> *btnArray;
@end

NSInteger colCount = 3;
NSInteger btnCount = 6;


@implementation SinaHomeComposeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(NSMutableArray<SinaHomeComposeButton *> *)btnArray{
    if(!_btnArray){
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

-(void)setupUI{
    
    UIImageView *backImageV = [[UIImageView alloc]init];
    [self addSubview:backImageV];
    _backImageV = backImageV;
    [backImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    //添加六个按钮
    [self addSendBtn];
    
    
}


#pragma mark
#pragma mark - 添加六个发送按钮
-(void)addSendBtn{
    
    CGFloat btnOffsetY = kScreenHeight;
    
    for (int i = 0; i < btnCount; i++) {
        
        CGFloat btnH = kComposeBtnH;
        CGFloat btnW = kComposeBtnW;
        CGFloat margin = (kScreenWidth - 3 * btnW) / 4;
        
        SinaHomeComposeButton *btn = [[SinaHomeComposeButton alloc]init];
        [self.btnArray addObject:btn];
        [self addSubview:btn];
        
        [btn setImage:[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateNormal];
        [btn setTitle:@"发布微博" forState:UIControlStateNormal];
        
        //确定几行几列
        NSInteger row = i / 3;
        NSInteger col = i % 3;
        
        CGFloat btnX = margin + (btnW + margin) * col;
        CGFloat btnY = (btnH + margin) * row + btnOffsetY;
        
        CGRect btnFrame = CGRectMake(btnX, btnY, btnW, btnH);
        
        btn.frame = btnFrame;
        
        [btn addTarget:self action:@selector(sentBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self startAnimationWithButton:btn index:i isShow:YES];
    }
    
    
}



#pragma mark
#pragma mark - 点击按钮触发方法
-(void)sentBtnDidClick:(UIButton *)btn{
    
    [UIView animateWithDuration:0.5 animations:^{
        btn.transform = CGAffineTransformMakeScale(2.0, 2.0);
        btn.alpha = 0.1;
        for (UIButton *otherBtn in _btnArray) {
            if(otherBtn != btn){
                otherBtn.transform = CGAffineTransformMakeScale(0.2, 0.2);
                otherBtn.alpha = 0.1;
            }
        }
    }completion:^(BOOL finished) {
        
        //跳转撰写控制器
        NSString *vcName = @"SinaHomeComposeController";
        Class clz = NSClassFromString(vcName);
        SinaHomeComposeController *composeVC = [[clz alloc]init];
        UINavigationController *composeNavi = [[UINavigationController alloc]initWithRootViewController:composeVC];
        [kKeyWindow.rootViewController presentViewController:composeNavi animated:YES completion:^{
            [self removeFromSuperview];
        }];
        
    }];
    
    
}



#pragma mark
#pragma mark - 开始动画
-(void)startAnimationWithButton:(UIButton *)btn index:(NSInteger)index isShow:(BOOL)isShow{
    
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    anim.springSpeed = 10;
    anim.springBounciness = 10;
    
    anim.beginTime = CACurrentMediaTime() + index * 0.05;
    
    CGFloat centerY = btn.center.y + (isShow ? -350 : 350);
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(btn.center.x, centerY)];
    
    [btn pop_addAnimation:anim forKey:nil];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.btnArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self startAnimationWithButton:obj index:idx isShow:NO];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
    
    
}


-(void)setBackImage:(UIImage *)backImage{
    
    _backImage = backImage;
    _backImageV.image = backImage;
    
}


@end
